﻿// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

namespace MidLevelBatchWriteItemExample
{
  using System;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using Amazon.DynamoDBv2;
  using Amazon.DynamoDBv2.DocumentModel;

  // snippet-start:[dynamodb.dotnet35.MidLevelBatchWriteItem]

  /// <summary>
  /// Shows how to use mid level Amazon DynamoDB API calls to perform batch
  /// operations. The example was created using the AWS SDK for .NET version
  /// 3.7 and .NET Core 5.0.
  /// </summary>
  public class MidLevelBatchWriteItem
  {
    public static async Task Main()
    {
      IAmazonDynamoDB client = new AmazonDynamoDBClient();

      await SingleTableBatchWrite(client);
      await MultiTableBatchWrite(client);
    }

    /// <summary>
    /// Perform a batch operation on a single DynamoDB table.
    /// </summary>
    /// <param name="client">An initialized DynamoDB object.</param>
    public static async Task SingleTableBatchWrite(IAmazonDynamoDB client)
    {
      Table productCatalog = Table.LoadTable(client, "ProductCatalog");
      var batchWrite = productCatalog.CreateBatchWrite();

      var book1 = new Document();
      book1["Id"] = 902;
      book1["Title"] = "My book1 in batch write using .NET helper classes";
      book1["ISBN"] = "902-11-11-1111";
      book1["Price"] = 10;
      book1["ProductCategory"] = "Book";
      book1["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" };
      book1["Dimensions"] = "8.5x11x.5";
      book1["InStock"] = new DynamoDBBool(true);
      book1["QuantityOnHand"] = new DynamoDBNull(); //Quantity is unknown at this time

      batchWrite.AddDocumentToPut(book1);

      // Specify delete item using overload that takes PK.
      batchWrite.AddKeyToDelete(12345);
      Console.WriteLine("Performing batch write in SingleTableBatchWrite()");
      await batchWrite.ExecuteAsync();
    }

    /// <summary>
    /// Perform a batch operation involving multiple DynamoDB tables.
    /// </summary>
    /// <param name="client">An initialized DynamoDB client object.</param>
    public static async Task MultiTableBatchWrite(IAmazonDynamoDB client)
    {
      // Specify item to add in the Forum table.
      Table forum = Table.LoadTable(client, "Forum");
      var forumBatchWrite = forum.CreateBatchWrite();

      var forum1 = new Document();
      forum1["Name"] = "Test BatchWrite Forum";
      forum1["Threads"] = 0;
      forumBatchWrite.AddDocumentToPut(forum1);

      // Specify item to add in the Thread table.
      Table thread = Table.LoadTable(client, "Thread");
      var threadBatchWrite = thread.CreateBatchWrite();

      var thread1 = new Document();
      thread1["ForumName"] = "S3 forum";
      thread1["Subject"] = "My sample question";
      thread1["Message"] = "Message text";
      thread1["KeywordTags"] = new List<string> { "S3", "Bucket" };
      threadBatchWrite.AddDocumentToPut(thread1);

      // Specify item to delete from the Thread table.
      threadBatchWrite.AddKeyToDelete("someForumName", "someSubject");

      // Create multi-table batch.
      var superBatch = new MultiTableDocumentBatchWrite();
      superBatch.AddBatch(forumBatchWrite);
      superBatch.AddBatch(threadBatchWrite);
      Console.WriteLine("Performing batch write in MultiTableBatchWrite()");

      // Execute the batch.
      await superBatch.ExecuteAsync();
    }
  }

  // snippet-end:[dynamodb.dotnet35.MidLevelBatchWriteItem]
}
